<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 22:38
 */

namespace app\api\model;
use think\Model;

class Area extends Model
{
    /**
    * 通过参数获取地址
    * @param param array 参数数组
    */
    public function _getArea($param){
        if(empty($param)){
            $this->error = "参数异常";
            return false;
        }
        $province = Db::name('area')->field('id,name,pid')->where(array('pid'=>1))->select();
        $data['province'] = $province;
        $pid = isset($param['pid']) ? $param['pid'] : $province[0]['id'];//所选择的省份id
        $city = Db::name('area')->field('id, name, pid')->where(array('pid'=>$pid))->select();
        $cid = isset($param['cid']) ? $param['cid'] : $city[0]['id'];
        $area = Db::name('area')->field('id, name, pid')->where(array('pid'=>$cid))->select();
        $data['city'] = $city;
        $data['area'] = $area;
        return $data;
    }
    
    /**
    * 通过id获取地址名
    * @param id int 地址id
    * @return string 地址名
    */
    public function _getAreaById($id){
        $data = $this->where([['id', '=', $id]])->find();
        if(empty($data)){
            $this->error = '该地址id不存在';
            return false;
        }
        
        return $data['name'];
    }
    

}